package xyz.molzhao.alink.base;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import xyz.molzhao.alink.exception.BusinessException;

/**
 * @author Molzhao
 * @Description:
 * @date 2022/2/20 15:20
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {
    private String code;

    private String message;

    private T data;

    public Result(ResultEnum resultEnum) {
        this.code = resultEnum.getCode();
        this.message = resultEnum.getDescription();
    }

    public static <T> Result<T> failed(BusinessException businessException) {
        return new Result<>(businessException.getCode(), businessException.getMessage(), null);
    }

    public static <T> Result<T> failed(ResultEnum resultEnum) {
        return new Result<>(resultEnum.getCode(), resultEnum.getDescription(), null);
    }

    public static <T> Result<T> failed(String message) {
        return new Result<>(ResultEnum.ERROR.getCode(), message, null);
    }
}
